﻿@page "/datagrid-footer-totals"

@using NorthwindBlazor.Data
@using NorthwindBlazor.Models.Northwind
@using Microsoft.EntityFrameworkCore

@inject NorthwindContext dbContext

<h1>DataGrid Footer Totals</h1>

<p>This page demonstrates fetching data from the server using <b>IQueryable</b> bound to a <b>DataGrid</b> with totals in column footers.</p>

<RadzenExample Name="DataGridFooterTotals" Heading="false" Documentation="false">
@if (orders == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <RadzenGrid @ref="ordersGrid" AllowFiltering="true" AllowPaging="true" PageSize="3" AllowSorting="true" 
                Data="@orders" TItem="Order" Value="@order">
        <Columns>
            <RadzenGridColumn Width="200px" TItem="Order" Property="OrderID" Title="Order ID">
                <FooterTemplate>
                    Total orders: <b>@orders.Count()</b>
                </FooterTemplate>
            </RadzenGridColumn>
            <RadzenGridColumn Width="200px" TItem="Order" Property="Customer.CompanyName" Title="Customer" />
            <RadzenGridColumn TItem="Order" Property="Employee.LastName" Title="Employee">
                <Template Context="order">
                    <div>@order.Employee?.LastName</div>
                    <RadzenImage Path="@order.Employee?.Photo" Style="width:150px" />
                </Template>
            </RadzenGridColumn>
            <RadzenGridColumn TItem="Order" Property="OrderDate" Title="Order Date">
                <Template Context="order">
                    @String.Format("{0:d}", order.OrderDate)
                </Template>
                <FooterTemplate>
                    Last order date: <b>@String.Format("{0:d}", orders.OrderByDescending(o => o.OrderDate).Last().OrderDate)</b>
                </FooterTemplate>
            </RadzenGridColumn>
            <RadzenGridColumn TItem="Order" Property="Freight" Title="Freight">
                <Template Context="order">
                    @String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", order.Freight)
                </Template>
                <FooterTemplate>
                    Total amount: <b>@String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", orders.Sum(o => o.Freight))</b>
                </FooterTemplate>
            </RadzenGridColumn>
            <RadzenGridColumn TItem="Order" Property="ShipName" Title="Ship Name" />
        </Columns>
    </RadzenGrid>
}
</RadzenExample>

@code {
    RadzenGrid<Order> ordersGrid;

    Order order;
    IEnumerable<Order> orders;
    IEnumerable<Customer> customers;

    protected override async Task OnInitializedAsync()
    {
        customers = await Task.FromResult(dbContext.Customers);

        orders = await Task.FromResult(from order in dbContext.Orders
        .Include("Customer")
        .Include("Employee")
        .Include("OrderDetails")
        .Include("OrderDetails.Product")
                                       select order);
        order = orders.FirstOrDefault();

    }

    void EditRow(Order order)
    {
        ordersGrid.EditRow(order);
    }

    void UpdateRow(Order order)
    {
        ordersGrid.UpdateRow(order);

        dbContext.Update<Order>(order);
        dbContext.SaveChanges();
    }

    void CancelEdit(Order order)
    {
        ordersGrid.CancelEditRow(order);

        var orderEntry = dbContext.Entry(order);
        orderEntry.CurrentValues.SetValues(orderEntry.OriginalValues);
        orderEntry.State = EntityState.Unchanged;
    }

    void DeleteRow(Order order)
    {
        dbContext.Remove<Order>(order);
        dbContext.SaveChanges();
    }
}
